import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:my_app/base/foundation/flutter_extension.dart';

// 画的关系分数
class DrawScoreWidget extends StatelessWidget {
  const DrawScoreWidget({super.key});

  @override
  Widget build(BuildContext context) {
    List<List<double>> listTotal = calculatePoint();

    double oneX = listTotal[0][0] + 10;
    double oneY = listTotal[0][1] - 10;

    double twoX = listTotal[1][0] - 13;
    double twoY = listTotal[1][1] + 8;

    double threeX = listTotal[2][0] - 40;
    double threeY = listTotal[2][1] - 10;

    double fourX = listTotal[3][0] - 40;
    double fourY = listTotal[3][1] - 10;

    double fiveX = listTotal[4][0] - 13;
    double fiveY = listTotal[4][1] - 22;

    double sixX = listTotal[5][0] + 10;
    double sixY = listTotal[5][1] - 15;

    return Stack(
      children: [
        SizedBox(
          width: 244.dp,
          height: 244.dp,
          child: CustomPaint(
            painter: HexagonPainter(false),
          ),
        ),
        SizedBox(
          width: 244.dp,
          height: 244.dp,
          child: CustomPaint(
            painter: HexagonPainter(true),
          ),
        ),
        SizedBox(
          width: 244.dp,
          height: 244.dp,
          child: CustomPaint(
            painter: HexagonPainter1(),
          ),
        ),
        Positioned(
            left: oneX,
            top: oneY,
            child: const Text(
              '第一',
              style: TextStyle(color: Color(0xFF333333), fontSize: 13),
            )),
        Positioned(
            left: twoX,
            top: twoY,
            child: const Text('第二',
                style: TextStyle(color: Color(0xFF333333), fontSize: 13))),
        Positioned(
            left: threeX,
            top: threeY,
            child: const Text('第三',
                style: TextStyle(color: Color(0xFF333333), fontSize: 13))),
        Positioned(
            left: fourX,
            top: fourY,
            child: const Text('第四',
                style: TextStyle(color: Color(0xFF333333), fontSize: 13))),
        Positioned(
            left: fiveX,
            top: fiveY,
            child: const Text('第五',
                style: TextStyle(color: Color(0xFF333333), fontSize: 13))),
        Positioned(
            left: sixX,
            top: sixY,
            child: const Text('第六',
                style: TextStyle(color: Color(0xFF333333), fontSize: 13))),
      ],
    );
  }

  List<List<double>> calculatePoint() {
    double centerX = 244.dp / 2;
    double centerY = 244.dp / 2;
    double radius = (244.dp) / 2.8;

    List<List<double>> list = [];
    for (int i = 0; i < 6; i++) {
      double angle = 2 * pi / 6 * i + (pi / 6);
      double x = centerX + radius * cos(angle);
      double y = centerY + radius * sin(angle);
      // tdLog('x $i----$x');
      // tdLog('y $i----$y');
      list.add([x, y]);
    }
    return list;
  }
}

class HexagonPainter extends CustomPainter {
  final bool isSub;

  HexagonPainter(this.isSub);

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = const Color(0xFF8965FE).withOpacity(0.6) //画笔颜色
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..isAntiAlias = true //是否启动抗锯齿
      ..style = PaintingStyle.stroke //绘画风格，默认为填充
      ..strokeWidth = 2; //画笔的宽度

    Paint subPaint = Paint()
      ..color = const Color(0xFFFF8924) //画笔颜色
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..isAntiAlias = true //是否启动抗锯齿
      ..strokeWidth = 2; //画笔的宽度

    Paint subPaintCircle = Paint()
      ..color = const Color(0xFFFF8924) //画笔颜色
      ..style = PaintingStyle.fill
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..isAntiAlias = true //是否启动抗锯齿
      ..strokeWidth = 2; //画笔的宽度

    Paint subFillPaint = Paint()
      ..color = const Color(0x1AFF8924) //画笔颜色
      ..style = PaintingStyle.fill
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..isAntiAlias = true //是否启动抗锯齿
      ..strokeWidth = 2; //画笔的宽度

    double centerX = size.width / 2;
    double centerY = size.height / 2;
    double radius = size.width / 2 * 0.7;

    Path path = Path();

    for (int i = 0; i < 6; i++) {
      double angle = 2 * pi / 6 * i + (pi / 6);
      double x = centerX + radius * cos(angle);
      double y = centerY + radius * sin(angle);
      // tdLog('x $i----$x');
      // tdLog('y $i----$y');

      if (isSub) {
        if (i == 0) {
          x = centerX + radius * 0.8 * cos(angle);
          y = centerY + radius * 0.8 * sin(angle);
        } else if (i == 1) {
          x = centerX + radius * 0.6 * cos(angle);
          y = centerY + radius * 0.6 * sin(angle);
        } else if (i == 2) {
          x = centerX + radius * 0.7 * cos(angle);
          y = centerY + radius * 0.7 * sin(angle);
        } else if (i == 3) {
          x = centerX + radius * 0.6 * cos(angle);
          y = centerY + radius * 0.6 * sin(angle);
        } else if (i == 4) {
          x = centerX + radius * 0.7 * cos(angle);
          y = centerY + radius * 0.7 * sin(angle);
        } else if (i == 5) {
          x = centerX + radius * 1 * cos(angle);
          y = centerY + radius * 1 * sin(angle);
        }
        canvas.drawCircle(Offset(x, y), 4, subPaintCircle);
      }

      if (i == 0) {
        path.moveTo(x, y);
      } else {
        path.lineTo(x, y);
      }
      canvas.drawLine(Offset(centerX, centerY), Offset(x, y), paint);
    }

    path.close();
    if (isSub) {
      canvas.drawPath(path, subFillPaint);
      canvas.drawPath(path, subPaint);
    } else {
      canvas.drawPath(path, paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

class HexagonPainter1 extends CustomPainter {
  HexagonPainter1();

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = const Color(0xFF8965FE).withOpacity(0.6) //画笔颜色
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..isAntiAlias = true //是否启动抗锯齿
      ..style = PaintingStyle.stroke //绘画风格，默认为填充
      ..strokeWidth = 2; //画笔的宽度

    double centerX = size.width / 2;
    double centerY = size.height / 2;
    double radius = size.width / 2 * 0.7 * 0.5;

    Path path = Path();

    for (int i = 0; i < 6; i++) {
      double angle = 2 * pi / 6 * i + (pi / 6);
      double x = centerX + radius * cos(angle);
      double y = centerY + radius * sin(angle);

      if (i == 0) {
        path.moveTo(x, y);
      } else {
        path.lineTo(x, y);
      }
    }

    path.close();
    drawDashedPath(canvas, paint, path);
  }

  void drawDashedPath(Canvas canvas, Paint paint, Path path) {
    PathMetrics pathMetrics = path.computeMetrics();
    for (PathMetric pathMetric in pathMetrics) {
      double distance = 0.0;
      bool draw = true;
      while (distance < pathMetric.length) {
        Path dashPath = pathMetric.extractPath(
          distance,
          distance + 1.0,
        );
        if (draw) {
          canvas.drawPath(dashPath, paint);
        }
        distance += 2;
        draw = !draw;
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
